﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using API.Models;
using PPG.DataAccess;
using PPG.CryptoProvider;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Web;


namespace API.Controllers
{
    public class ProcessRequestController : ApiController
    {
        /// <summary>
        /// Intializing Helper Classes - DB Connection and Security Provider for Encrypting and Decrypting Passwords
        /// Pass in DBServerName, DBUsern
        /// ame and DBPassword from Config File for DB connections
        /// The Security helper does nto take any parameters
        /// </summary>
        DataSourceCommunicator dsComm = new DataSourceCommunicator(DataSourceCommunicator.ParamType.ServerCredentials, ConfigurationManager.AppSettings["DBServerName"].ToString(), ConfigurationManager.AppSettings["DBUserName"].ToString(), ConfigurationManager.AppSettings["DBPassword"].ToString());

        [ActionName("ProcessRequest")]

        public AddReferralProcessResponse PostProcessRequest([FromBody]AddReferralProcessRequest request)
        {
            AddReferralProcessResponse response = new AddReferralProcessResponse();

            dsComm.AddParameter("@ParamReferralID", request.ReferralID);
            dsComm.AddParameter("@ParamRequstedPracticeID", request.Practice);
            dsComm.AddParameter("@ParamReferredPhysicianID", request.Physician);
            if (!String.IsNullOrEmpty(request.AcceptanceStatus))
            {
                dsComm.AddParameter("@ParamAcceptanceStatus", request.AcceptanceStatus);
            }
            dsComm.AddParameter("@ParamAcceptanceNotes", request.AcceptanceNotes);
            dsComm.AddParameter("@ParamScheduleStatus", request.ScheduleStatus);
            dsComm.AddParameter("@ParamScheduleNotes", request.ScheduleNotes);
            dsComm.AddParameter("@ParamVisitStatus", request.VisitStatus);
            dsComm.AddParameter("@ParamVisitNotes", request.VisitNotes);
            dsComm.AddParameter("@ParamLoginID", request.LoginID);
            dsComm.AddParameter("@ParamApptDate", request.AppointmentDate);
            dsComm.AddParameter("@ParamApptTime", request.AppointmentTime);
            dsComm.AddParameter("@ParamNoteStatus", request.NoteStatus);
            dsComm.AddParameter("@ParamNoteSentDate", request.NoteSentDate);

            int i = dsComm.ExecuteNonQuery("spAddEditReferralProcessStatus");

            if( i > 1)
            {
                response.SaveStauts = "Saved";
            }
            else
            {
                response.SaveStauts = "Failed";
            }
            return response;
        }

        public AddReferralProcessRequest GetProcessRequest(int id)
        {
            AddReferralProcessRequest response = new AddReferralProcessRequest();
            dsComm.AddParameter("@ParamReferralID", id);

            string data = dsComm.ExecuteStoredProcedure("dbo.spGetReferralProcessStatus");
            XmlTextReader xReader = new XmlTextReader(data, XmlNodeType.Document, null);
            DataSet ReferralDataSet = new DataSet();
            ReferralDataSet.ReadXml(xReader);

            if(ReferralDataSet.Tables.Count > 0)
            {
                DataRow dr = ReferralDataSet.Tables[0].Rows[0];
                response.AcceptanceNotes = dr["AcceptanceNotes"].ToString();
                if (bool.Parse(dr["AcceptanceStatus"].ToString()))
                {
                    response.AcceptanceStatus = dr["AcceptanceStatus"].ToString();
                }
                else
                {
                    response.AcceptanceStatus = "";
                }
                response.VisitNotes = dr["VisitNotes"].ToString();
                response.VisitStatus = bool.Parse(dr["VisitStatus"].ToString());
                response.ScheduleNotes = dr["ScheduleNotes"].ToString();
                response.ScheduleStatus = bool.Parse(dr["ScheduleStatus"].ToString());
                response.Physician = dr["ReferredPhysicianID"].ToString();
                response.Practice = dr["RequestPracticeID"].ToString();
                response.PreferredPhysician = dr["PreferredPhysicianID"].ToString();

                response.AssignedBy = dr["AssignedBy"].ToString();
                response.CreatedBy = dr["CreatedBy"].ToString();
                response.CreatedDate = dr["CreatedDate"].ToString();
                response.AcceptanceTime = dr["AcceptanceUpdatedTime"].ToString();
                response.AcceptanceUpdatedBy = dr["AcceptedBy"].ToString();
                response.ScheduleTime = dr["ScheduleUpdatedTime"].ToString();
                response.ScheduleUpdatedBy = dr["ScheduleUpdatedBy"].ToString();
                response.VisitTime = dr["VisitUpdatedTime"].ToString();
                response.VisitUpdatedBy = dr["VisitUpdatedBy"].ToString();
                response.AssignedTime = dr["AssignedTime"].ToString();
                response.AppointmentDate = dr["AppointmentDate"].ToString();
                response.AppointmentTime = dr["AppointmentTime"].ToString();
                response.NoteStatus = bool.Parse(dr["NoteStatus"].ToString());
                response.NoteUpdatedBy = dr["NoteUpdatedBy"].ToString();
                response.NoteUpdatedTime = dr["NoteUpdatedTime"].ToString();
                response.NoteSentDate = dr["NoteSentDate"].ToString();
                response.PPGPhysician = bool.Parse(dr["PPGPhysician"].ToString()) ? "Yes" : "No";
                response.EstablishedPatient = bool.Parse(dr["EstablishedPatient"].ToString()) ? "Yes" : "No";
                response.PrefPhone = dr["PrefPhone"].ToString();
                response.ErrorMessage = "Success";
            }
            else
            {
                response.ErrorMessage = "Failed";
            }


            return response;
        }

        /// <summary>
        /// Options Resposen for the preflight request of a CORS Opertaions. Will be standard across various functions
        /// </summary>
        /// <returns></returns>
        public HttpResponseMessage OptionsProcessRequest()
        {
            var response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
        }
    }
}
